package com.lg.system.menu.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lg.system.menu.dao.MenuDao;
import com.lg.system.menu.entity.Menu;
import com.lg.system.menu.service.MenuService;
import com.lg.system.role.service.RoleMenuService;
import com.lg.utils.BuildTree;
import com.lg.utils.Tree;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author dl
 * @since 2018-03-21
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements MenuService {
	
	@Autowired
	RoleMenuService roleMenuService;
	
	@Autowired
	MenuDao menuDao;
	
	public boolean save(Menu menu) {
		menu.setGmtCreate(new Date());
		return super.insert(menu);
	}

	@Override
	public boolean update(Menu menu) {
		menu.setGmtModified(new Date());
		return super.updateById(menu);
	}

	@Override
	public Tree<Menu> selectTreeList() {
		Map<String, Object> map=new HashMap<>(16);
		List<Menu> menus=selectByMap(map);
		if(menus!=null&&!menus.isEmpty()){
			List<Tree<Menu>> trees = new ArrayList<Tree<Menu>>();
			for (Menu sysMenuDO : menus) {
				Tree<Menu> tree = new Tree<Menu>();
				tree.setId(sysMenuDO.getMenuId().toString());
				tree.setParentId(sysMenuDO.getParentId().toString());
				tree.setName(sysMenuDO.getName());
				tree.setSpread(true);
				Map<String, Object> attributes = new HashMap<>(16);
				attributes.put("url", sysMenuDO.getUrl());
				attributes.put("icon", sysMenuDO.getIcon());
				attributes.put("perms", sysMenuDO.getPerms());
				attributes.put("type", sysMenuDO.getType());
				attributes.put("orderNum", sysMenuDO.getOrderNum());
				tree.setAttributes(attributes);
				trees.add(tree);
			}
			Tree<Menu> t = BuildTree.build(trees);
			return t;
		}else{
			return null;
		}
	}

	@Override
	public boolean deleteById(Long id) {
		Menu menu=selectById(id);
		if(menu.getType()==0||menu.getType()==1){//目录或者菜单需要递归删除
			List<Long> ids=new ArrayList<>();
			ids.add(id);
			selectChilden(id,ids);
			boolean f= super.deleteBatchIds(ids);
			if (f){//删除菜单或者目录成功时将删除，角色-菜单关系表
				for(Long menuId:ids){
					roleMenuService.deleteByMenuId(menuId);
				}
				return true;
			}	
		}
		else if(menu.getType()==2)//按钮
		{
			return super.deleteById(id);
		}
		return false;
	}
	
	private void selectChilden(Long id,List<Long> list){
		Map<String, Object> param=new HashMap<>();
		param.put("parent_id", id);
		List<Menu> menus=selectByMap(param);
		if(null!=menus&&!menus.isEmpty()){
			for(Menu menu:menus){
				list.add(menu.getMenuId());
				selectChilden(menu.getMenuId(),list);
			}
		}
	}

	@Override
	public List<Menu> selectMenuTreeByUid(Long userId) {
		return menuDao.selectMenuTreeByUid(userId);
	}

	@Override
	public Set<String> selectPermsByUid(Long userId) {
		    List<String> perms= menuDao.selectPermsByUid(userId);
			Set<String> permsSet = new HashSet<>();
			for (String perm : perms) {
				permsSet.addAll(Arrays.asList(perm.trim().split(",")));
			}
			return permsSet;
	}

	@Override
	public List<Menu> selectMenuUid(Long userId) {
		return menuDao.selectMenuUid(userId);
	}

}
